-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix header forwarding #28
Conversation
Pass excluded headers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small stuff thanks for seeing this through
], | ||
excludeRequestHeaders: [ | ||
// For single-site setups or hosting on multiple servers, block the host header | ||
'host' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right so it is critical to include this setting in the project level PRs.
README.md
Outdated
### `forwardHeaders` | ||
### `includeResponseHeaders` | ||
|
||
An array of HTTP headers that you want to include from Apostrophe to the final response sent to the browser - useful if you want to use an Apostrophe module like `@apostrophecms/security-headers` and want to keep those headers as configured in Apostrophe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also good for respecting apostrophe's caching headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
README.md
Outdated
At the present time, Astro is not compatible with the `nonce` property of `content-security-policy` `script-src` value. So this is automatically removed with that integration. The rest of the CSP header remains unchanged. | ||
|
||
### `excludeRequestHeaders` | ||
|
||
An array of HTTP headers that you want to prevent from being forwarded from the browser to Apostrophe. This is particularly useful in single-site setups where you want to block the `host` header to allow Astro and Apostrophe to run on different domains or ports. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just say different hostnames. Different ports aren't really a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
lib/aposResponse.js
Outdated
const requestHeaders = {}; | ||
for (const [name, value] of req.headers) { | ||
const headerLower = name.toLowerCase(); | ||
if (!config.excludeRequestHeaders?.map(h => h.toLowerCase()).includes(headerLower)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Shouldn't this be an equality comparison rather than "includes" which would match a header that happens to be a substring of some unrelated header?
- Because it is on each request, it is worth making it more efficient by converting all of the excluded request headers to lowercase at startup rather than doing it over and over like this.
- So you should be able to do a simple "includes" test on
config.excludeRequestHeaders
after you've normalized it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right! Good catch with the includes. Code refactored.
lib/aposResponse.js
Outdated
@@ -7,10 +7,12 @@ export default async function aposResponse(req) { | |||
const aposUrl = new URL(url.pathname, aposHost); | |||
aposUrl.search = url.search; | |||
|
|||
|
|||
const excludedHeadersLower = new Set(config.excludeRequestHeaders?.map(h => h.toLowerCase()) || []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not move this to top level code in this file so it doesn't have to happen on every request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Summary
Summarize the changes briefly, including which issue/ticket this resolves. If it closes an existing Github issue, include "Closes #[issue number]"
This PR refactors the handling of headers to allow for the excluding of the host header in single-site, multi-server instances. It also renames the header configuration arrays while maintaining backward compatibility.
What are the specific steps to test this change?
For example:
This has been tested in a Heroku two-server deployment. It has been tested in local dev deployment. It has not been tested in either Apostrophe hosting. It will be tested in multisite local deployment
What kind of change does this PR introduce?
(Check at least one)
Make sure the PR fulfills these requirements:
If adding a new feature without an already open issue, it's best to open a feature request issue first and wait for approval before working on it.
Other information: